//
//  PersonalCenterController.m
//  lhpc_manager
//
//  Created by 赵永杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "PersonalCenterController.h"
#import "PersonalHeaderView.h"
#import "LHNetworking.h"
#import "PasswordSettingViewController.h"
#import "WithDrawHistoryViewController.h"
#import "ResetPasswordViewController.h"
#import "LoginViewController.h"

@interface PersonalCenterController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) PersonalHeaderView *header;

@property (nonatomic, strong) UIButton *signOutBtn;

@property (nonatomic, assign) BOOL hasSetPassword;

@end

@implementation PersonalCenterController

#pragma mark - LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"个人中心";
    
    [self setupChildViews];
    
    [self requestData];
}


#pragma mark - Public

#pragma mark - Private

- (void)setupChildViews {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.header setFrame:CGRectMake(0, 0, kScreenW, 150)];
    self.tableView.tableHeaderView = self.header;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 75)];
    footer.backgroundColor = BACKCOLOR;
    self.signOutBtn.frame = CGRectMake(20, 35, kScreenW - 40, 40);
    [footer addSubview:self.signOutBtn];
    self.tableView.tableFooterView = footer;
}

- (void)requestData {
    LHPC_WEAK_SELF;
    
    [HLProgressHUD showLodingHudWithText:nil view:self.view];
    [[LHNetworking shared] POST:LHSetRequestAPI(LHPersonalCenter) parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        [HLProgressHUD dismissMBHud:strongSelf.view];
        
        if (responseObject) {
            NSDictionary *dic = responseObject[@"data"];
            [strongSelf handleResultWithParams:dic];
            
            strongSelf.hasSetPassword = [dic[@"password"] boolValue];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)handleResultWithParams:(NSDictionary *)dic {
    [self.header updateUserInfo:dic];
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"setting"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"setting"];
    }
    NSArray *imageArr = @[@"cashHistory", @"password"];
    NSArray *titleArr = @[@"提现记录", @"密码管理"];
    cell.textLabel.text = titleArr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:imageArr[indexPath.row]];
    cell.textLabel.textColor = BLACKCOLOR;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    head.backgroundColor = BACKCOLOR;
    return head;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // 提现记录
        
        WithDrawHistoryViewController * view = [[WithDrawHistoryViewController alloc]init];
        
        [self.navigationController pushViewController:view animated:YES];
        
    }else{
        // 密码管理
        if (self.hasSetPassword) {
            PasswordSettingViewController * view = [[PasswordSettingViewController alloc]init];
            [self.navigationController pushViewController:view animated:YES];
            
        }else{
            //第一次设置支付密码
            ResetPasswordViewController *vc = [[ResetPasswordViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
    }
    
}

- (void)signOutAction {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"退出后不会删除任何历史数据，下次登录依然可以使用本帐号。" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *signOut = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        LHSetUserToken(@"");
        LHSetUserID(@"");
        LHSetUserPhone(@"");
        
        LoginViewController *vc = (LoginViewController *)LHGetSB_VC(@"Login");
        UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:vc];
        
        [self presentViewController:nv animated:YES completion:^{
        }];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:signOut];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - Getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = BACKCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (PersonalHeaderView *)header {
    if (!_header) {
        _header = [[PersonalHeaderView alloc] init];
    }
    return _header;
}

- (UIButton *)signOutBtn {
    if (!_signOutBtn) {
        _signOutBtn = [[UIButton alloc] init];
        _signOutBtn.backgroundColor = [UIColor whiteColor];
        [_signOutBtn setTitle:@"注销" forState:UIControlStateNormal];
        [_signOutBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
        [_signOutBtn addTarget:self action:@selector(signOutAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _signOutBtn;
}

#pragma mark - Setter

@end
